{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {
    "raw_mimetype": "text/restructuredtext"
   },
   "source": [
    ".. _nb_decomposition:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Decomposition"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Also, multi-objective problems can be decomposed using a scalarization function.\n",
    "In the following, the contour lines of different methods are shown.\n",
    "\n",
    "Let us first make the necessary imports and define the points in the design space:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from pymoo.util.misc import all_combinations\n",
    "\n",
    "# number of points to be used for plotting\n",
    "n_points = 100\n",
    "\n",
    "# the xlim\n",
    "P = np.linspace(0, 3, n_points)\n",
    "\n",
    "# used for the meshgrid\n",
    "X = all_combinations(P,P)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A method to plot the contours:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "def plot_contour(X, F):\n",
    "    _X = X[:, 0].reshape((n_points,n_points))\n",
    "    _Y = X[:, 1].reshape((n_points,n_points))\n",
    "    _Z = F.reshape((n_points,n_points))\n",
    "    \n",
    "    fig, ax = plt.subplots()\n",
    "    ax.contour(_X,_Y, _Z, colors='black')\n",
    "    ax.arrow(0, 0, 2.5, 2.5, color='blue', head_width=0.1, head_length=0.1, alpha=0.4)\n",
    "    ax.set_aspect('equal')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And then define the weights to be used by the decomposition functions:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "weights = [0.5, 0.5]"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {
    "raw_mimetype": "text/restructuredtext"
   },
   "source": [
    ".. _nb_weighted_sum:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Weighted Sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from pymoo.decomposition.weighted_sum import WeightedSum\n",
    "\n",
    "dm = WeightedSum()\n",
    "plot_contour(X, dm(X, weights=weights))"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {
    "raw_mimetype": "text/restructuredtext"
   },
   "source": [
    ".. _nb_tchebyshev:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Tchebysheff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from pymoo.decomposition.tchebicheff import Tchebicheff\n",
    "\n",
    "dm = Tchebicheff()\n",
    "plot_contour(X, dm(X, weights=weights))"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {
    "raw_mimetype": "text/restructuredtext"
   },
   "source": [
    ".. _nb_asf:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Achievement Scalarization Function (ASF)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Details can be found in <cite data-cite=\"asf\"></cite>."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from pymoo.decomposition.asf import ASF\n",
    "\n",
    "dm = ASF(eps=0.0)\n",
    "\n",
    "plot_contour(X, dm(X, weights=weights))"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {
    "raw_mimetype": "text/restructuredtext"
   },
   "source": [
    ".. _nb_aasf:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Augmented Achievement Scalarization Function (AASF)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Details can be found in <cite data-cite=\"aasf\"></cite>."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from pymoo.decomposition.aasf import AASF\n",
    "\n",
    "dm = AASF(eps=0.0, beta=5)\n",
    "\n",
    "plot_contour(X, dm(X, weights=weights))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "dm = AASF(eps=0.0, beta=25)\n",
    "\n",
    "plot_contour(X, dm(X, weights=weights))"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {
    "raw_mimetype": "text/restructuredtext"
   },
   "source": [
    ".. _nb_pbi:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### PBI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from pymoo.decomposition.pbi import PBI\n",
    "\n",
    "dm = PBI(eps=0.0, theta=0.5)\n",
    "\n",
    "plot_contour(X, dm(X, weights=weights))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "dm = PBI(eps=0.0, theta=1.0)\n",
    "plot_contour(X, dm(X, weights=weights))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "dm = PBI(eps=0.0, theta=5.0)\n",
    "plot_contour(X, dm(X, weights=weights))"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
